package com.erp.erpboot.controller.base;

import com.alibaba.fastjson.JSON;
import com.erp.erpboot.factory.ApplicationContextFactory;
import com.erp.erpboot.mapper.VendorClassMapper;
import com.erp.erpboot.pojo.VendorClass;
import com.erp.erpboot.pojo.DataObject;
import com.erp.erpboot.pojo.TreeObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/vendorClass")
public class VendorClassController {
    @Autowired
    VendorClassMapper vendorClassMapper;

    ApplicationContext context = ApplicationContextFactory.getApplicationContext();

    @RequestMapping({"/", "/index"})
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView("vendorclass/index");
        return mav;
    }

    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(HttpServletRequest request, HttpServletResponse response) throws Exception {
        ModelAndView mav = new ModelAndView("vendorclass/create");
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public DataObject create(VendorClass vendorClass) throws Exception {
        vendorClassMapper.add(vendorClass);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    @RequestMapping(value = {"/update"}, method = RequestMethod.GET)
    public ModelAndView update(int id) throws Exception {
        ModelAndView mav = new ModelAndView("vendorclass/update");
        VendorClass vendorClass = vendorClassMapper.get(id);
        mav.addObject("data", JSON.toJSONString(vendorClass));
        return mav;
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public DataObject update(VendorClass vendorClass) throws Exception {
        vendorClassMapper.update(vendorClass);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public DataObject delete(int id) throws Exception {
        vendorClassMapper.delete(id);

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        return dataObject;
    }

    private List<TreeObject> initTree(int parentID){
        List<VendorClass> vendorClassList=vendorClassMapper.getByParentID(parentID);
        List<TreeObject> treeObjects=new ArrayList<>();

        for (VendorClass vendorClass : vendorClassList) {
            TreeObject treeObject=new TreeObject();
            treeObject.setId(vendorClass.getiID());
            treeObject.setTitle(vendorClass.getcVenCCode() + " " + vendorClass.getcVenCName());
            treeObject.setChildren(initTree(vendorClass.getiID()));
            treeObjects.add(treeObject);
        }
        return treeObjects;
    }

    @ResponseBody
    @RequestMapping("/getTree")
    public DataObject getTree() throws Exception {
        List<TreeObject> treeObjects = initTree(0);

        DataObject dataObject = (DataObject) context.getBean("dataObject");
        dataObject.setData(treeObjects);

        return dataObject;
    }

    @ResponseBody
    @RequestMapping("/getByID")
    public String getByID(int id) throws Exception {

        DataObject dataObject = (DataObject) context.getBean("dataObject");

        VendorClass data = vendorClassMapper.get(id);
        dataObject.setData(data);
        dataObject.setCount(1);
        return JSON.toJSONString(dataObject);
    }

    @ResponseBody
    @RequestMapping("/getListWithPage")
    public String getListWithPage(int page, int limit) throws Exception {

        DataObject dataObject = (DataObject) context.getBean("dataObject");
        List<VendorClass> data = vendorClassMapper.listByPage(limit, page);
        dataObject.setData(data);
        dataObject.setCount(vendorClassMapper.count());
        return JSON.toJSONString(dataObject);
    }
}
